#include "gamedialog.h"
#include<QPainter>
#include<QStyleOption>
#include "mainwindow.h"
/*

  GAME DIALOG BUTTON CLASS


  */

GameDialogButton::GameDialogButton(QGraphicsObject *parent):
    QGraphicsObject(parent)
{
    setAcceptHoverEvents(true);
}

QRectF GameDialogButton::boundingRect() const{
    return QRectF(0,0,BUTTON_WIDTH, BUTTON_HEIGHT);
}

void GameDialogButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    bool down = option->state & QStyle::State_Sunken;
    bool over = option->state & QStyle::State_MouseOver;
    if(down){
        QBrush b(QColor(Qt::white), Qt::SolidPattern);
        painter->fillRect(boundingRect(), b);
    }else if(over){
        QBrush b(QColor(Qt::black), Qt::SolidPattern);
        painter->fillRect(boundingRect(), b);
    }else{
        QBrush b(QColor(Qt::green), Qt::SolidPattern);
        painter->fillRect(boundingRect(), b);
    }
    painter->drawText(boundingRect(), Qt::AlignCenter, "Button");
}


void GameDialogButton::mousePressEvent(QGraphicsSceneMouseEvent *event){
    emit pressed();
    update();
}
void GameDialogButton::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
    update();
}

/*

  GAME DIALOG CLASS


  */



GameDialog::GameDialog(QGraphicsItem* parent)
    : QGraphicsObject(parent)
{
    e = new QEventLoop(this);
    //instantiate
    acceptButton = new GameDialogButton(this);
    rejectButton = new GameDialogButton(this);
    acceptButton->setPos(-65, 0);

    //connect button signal and slot
    connect(acceptButton, SIGNAL(pressed()), this, SLOT(accept()));
    connect(rejectButton, SIGNAL(pressed()), this, SLOT(reject()));

    //start animation when created
    QPointF old = this->pos();
    this->setPos(-20,0);
    GameParAnimation* anim = new GameParAnimation(this);
    anim->addFade(this, 0, 1, 300);
    anim->addMove(this, pos(), old, 300);
    anim->start(QAbstractAnimation::DeleteWhenStopped);
}



QRectF GameDialog::boundingRect() const{
    return QRectF(-DIALOG_WIDTH/2, -DIALOG_HEIGHT/2, DIALOG_WIDTH, DIALOG_HEIGHT);
}

void GameDialog::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->fillRect(boundingRect(), Qt::red);
}

//when call, go into local event loop. i.e. the global stuff will stop processing events until exit(int returnValue)
//is called to release it, and return the value
//here, when button is clicked, signal pressed() emited, hence slot accept() or reject() invoked, then release lock and return
//result value to test.
int GameDialog::exec(){
    int test = e->exec();
    return test;
}

void GameDialog::accept(){
    e->exit(0);
}

void GameDialog::reject(){
    e->exit(-1);
}


/*

  ACTION GUIDE DIALOG CLASS

  */


ActionGuideDialog::ActionGuideDialog(QGraphicsObject* parent, const QString &text)
    :QGraphicsObject(parent), displayText(text)
{
    GameSeqAnimation* initAnim = new GameSeqAnimation(this);
    QPointF e = pos();
    this->moveBy(0, GUIDEBAR_HEIGHT);
    QPointF s = pos();
    initAnim->addMove(this, s, e, 200);
    initAnim->start(QAbstractAnimation::DeleteWhenStopped);
}

QRectF ActionGuideDialog::boundingRect() const{
    return QRectF(-MainWindow::SCENE_WIDTH/2, MainWindow::SCENE_HEIGHT/2-GUIDEBAR_HEIGHT,
                  MainWindow::SCENE_WIDTH, GUIDEBAR_HEIGHT);
}

void ActionGuideDialog::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->fillRect(boundingRect(), Qt::black);
    painter->setPen(QPen(Qt::white));
    QFont f;
    f.setPointSize(15);
    f.setFamily("Garamond");
    painter->setFont(f);

    painter->drawText(boundingRect(), Qt::AlignVCenter | Qt::AlignLeft, displayText);
}



void ActionGuideDialog::setGuideText(QString text){
    displayText = text;
    update();
}
